﻿using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.Linq;
using System.Xml.Linq;
using System.Net;
using Newtonsoft.Json;
using System.Runtime.Serialization;
using KOF.Helpers;
using KOF.ViewModels;
using KOF.Models;
using KOF.ViewModels.Commands;
using System.Windows.Navigation;
using System.Collections;
using System.Threading;
using System.IO.IsolatedStorage;
using System.Windows.Threading;
using Microsoft.Phone.Controls;
using Microsoft.Xna.Framework.Media;
using System.IO;




namespace KOF.ViewModels
{
    public class MainViewModel : INotifyPropertyChanged
    {
       
        int pageNo;
      
        public MainViewModel()
        {
            this.ListItemSource = new ObservableCollection<ReleaseResult>();
        }

      
        public ObservableCollection<ReleaseResult> ListItemSource { get; private set; }

        public bool IsDataLoaded
        {
            get;
             set;
        }
        public bool IsLoad
        {
            get;
            set;
        }
        public void LoadData()
        {
             pageNo = 1;
            ListItemSource.Clear();
            getReleaseList();
            this.IsDataLoaded = true;
            IsLoad = false;
           
        }

        public event PropertyChangedEventHandler PropertyChanged;

        

       
        public void getReleaseList()
        {
            string releaseStatus = string.Empty;
            if (IsolatedStorageSettings.ApplicationSettings.Contains("ReleaseStatus"))
            {
                if (Convert.ToInt16(IsolatedStorageSettings.ApplicationSettings["ReleaseStatus"]) == 0)
                {
                    releaseStatus = "past";
                }
                else
                {
                    releaseStatus = "upcoming";
                }
            }
            else
            {
                releaseStatus = "upcoming";
            }

            string strURL = "http://app.kicksonfire.com/kofapp/index.php/api/events/EventList?event_type=" + releaseStatus;
            ObservableCollection<ResponseBrand> lstBrnds = null;
            string strTypeFilter = string.Empty;
            if (IsolatedStorageSettings.ApplicationSettings.Contains("SelectedBrands"))
            {
                string temp = string.Empty;
                lstBrnds = (ObservableCollection<ResponseBrand>)IsolatedStorageSettings.ApplicationSettings["SelectedBrands"];
                foreach (ResponseBrand brand in lstBrnds)
                {
                    if (brand.id != "0") temp += brand.id + ",";
                }
                if (temp.EndsWith(",")) temp = temp.Remove(temp.Length - 1, 1);
                strTypeFilter = "&type=" + temp;
            }


            WebClient wc = new WebClient();
            wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
            strURL += strTypeFilter + "&page=" + pageNo++;
            wc.DownloadStringAsync(new Uri(strURL));
        }

        void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            try
            {
                if (e.Result.Contains("Data Not Found")) return;
                var deserialized = JsonConvert.DeserializeObject<ServiceResult>(e.Result);
                int i = 1;

            

                foreach (ReleaseResult item in deserialized.data)
                {
                    ReleaseResult temp = new ReleaseResult();
                    temp.id = item.id;
                    temp.title = item.title;
                    temp.description = item.description;
                    temp.price = item.price;
                    temp.style = item.style;
                    temp.iphone_images = item.iphone_images;
                    temp.event_date = item.event_date;
                    temp.master_image = item.master_image;
                    temp.type = item.type;
                    temp.thumb_iphone_master_image = item.thumb_iphone_master_image;
                    temp.IsDownloading = true;
                    temp.kof_link = item.kof_link;
                   
                    (App.Current as App).lstReleaseResult.Add(temp);

                    
                    if (item.title.Length > 28)
                    {
                        item.title = item.title.Substring(0, 24) + "...";
                    }
                    if (item.style.Length > 28)
                    {
                        item.style = item.style.Substring(0, 24) + "...";
                    }
                    if (!string.IsNullOrWhiteSpace(item.price) && item.price != "N/A")
                    {
                        item.price = "$" + item.price;
                    }
                    i++;


                    item.IsDownloading = true;
                   
                    item.iphone_images.Add(item.master_image);

                    if (item.iphone_images.Count > 0)
                    {
                        string tempimg = item.iphone_images[0].ToString();
                        item.iphone_images[0] = item.iphone_images[item.iphone_images.Count - 1];
                        item.iphone_images[item.iphone_images.Count - 1] = tempimg;
                    }
                    if (ListItemSource == null)
                    {
                        ListItemSource = new System.Collections.ObjectModel.ObservableCollection<ReleaseResult>();
                    }
                    ListItemSource.Add(item);
                }
                //this.IsLoader = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
          
        }

        private void NotifyPropertyChanged(String propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (null != handler)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}



